package com.lxmes.gs.service.impl;

import java.util.List;
import com.lxmes.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.common.utils.StringUtils;
import com.lxmes.common.utils.bean.BeanValidators;
import com.lxmes.common.exception.ServiceException;
import javax.validation.Validator;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lxmes.gs.mapper.ProjectStaffMapper;
import com.lxmes.gs.domain.ProjectStaff;
import com.lxmes.gs.service.IProjectStaffService;

/**
 * 项目人员Service业务层处理
 * 
 * @author wdf
 * @date 2023-04-24
 */
@Service
public class ProjectStaffServiceImpl extends ServiceImpl<ProjectStaffMapper,ProjectStaff> implements IProjectStaffService{
    
    @Autowired
    private ProjectStaffMapper projectStaffMapper;
    @Autowired
    protected Validator validator;
    /**
     * 查询项目人员
     * 
     * @param id 项目人员主键
     * @return 项目人员
     */
    @Override
    public ProjectStaff selectProjectStaffById(Long id){
        return projectStaffMapper.selectById(id);
    }

    /**
     * 查询项目人员列表
     * 
     * @param projectStaff 项目人员
     * @return 项目人员
     */
    @Override
    public List<ProjectStaff> selectProjectStaffList(ProjectStaff projectStaff){
        return projectStaffMapper.selectList(Wrappers.lambdaQuery(projectStaff));
    }

    /**
     * 新增项目人员
     * 
     * @param projectStaff 项目人员
     * @return 结果
     */
    @Override
    public int insertProjectStaff(ProjectStaff projectStaff){
        projectStaff.setCreateTime(DateUtils.getNowDate());
        return projectStaffMapper.insert(projectStaff);
    }

    /**
     * 修改项目人员
     * 
     * @param projectStaff 项目人员
     * @return 结果
     */
    @Override
    public int updateProjectStaff(ProjectStaff projectStaff){
        projectStaff.setUpdateTime(DateUtils.getNowDate());
        return projectStaffMapper.updateById(projectStaff);
    }

    /**
     * 批量删除项目人员
     * 
     * @param ids 需要删除的项目人员主键
     * @return 结果
     */
    @Override
    public int deleteProjectStaffByIds(Long[] ids){
        return projectStaffMapper.deleteProjectStaffByIds(ids);
    }

    /**
     * 删除项目人员信息
     * 
     * @param id 项目人员主键
     * @return 结果
     */
    @Override
    public int deleteProjectStaffById(Long id){
        return projectStaffMapper.deleteById(id);
    }
    @Override
    public String imporIteminfo(List<ProjectStaff> projectStaffList, boolean updateSupport) {
        if (StringUtils.isNull(projectStaffList) || projectStaffList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (ProjectStaff projectStaff : projectStaffList)
        {
            try
            {
                BeanValidators.validateWithException(validator, projectStaff);
                this.insertProjectStaff(projectStaff);
                successNum++;
                successMsg.append("<br/>" + successNum + "数据导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/> 数据导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
